﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace JavaScriptCodeBehind
{
    public class MetaProgrammingData
    {

        public MetaProgrammingData(Control control)
        {
            this.control = control;
            if (this.control.Controls.Count > 0)
                this.hashTable = new Hashtable();
        }
                
        public Hashtable hashTable = null;
        public Control control;
        public bool isRoot = false;
        public bool inRoot = false;

        public override string ToString()
        {
            if (this.control.Visible)
            {
                string a = string.Empty;

                // Parameter #0: Control's server id
                // Parameter #1: Control's constructor
                if ((this.control is JavaScriptCodeBehind.UserControl) || (this.control is JavaScriptCodeBehind.MasterPage))
                    a = "['" + this.control.ID + "', function(){var a=" + this.control.ClientID + "=new " + this.control.GetType().BaseType.Name + "();" +
                        "a.ServerID='" + this.control.ID + "';return a}";
                else if (this.control is JavaScriptCodeBehind.Page)
                    a = "['Page', function(){var a=typeof(Page)!=='undefined'?Page:Page=new " + this.control.GetType().BaseType.Name + "();" +
                        "a.ServerID='';return a}";
                else if (this.control is System.Web.UI.Page)
                    a = "['Page', null";
                else
                    a = "['" + this.control.ID + "', null";

                // Parameter #2: Child controls
                a += ",[";
                if (this.hashTable != null)
                {
                    ArrayList keys = new ArrayList(this.hashTable.Keys);
                    keys.Sort();
                    for (int i = 0; i < keys.Count; i++)
                    {
                        MetaProgrammingData metaProgrammingData = (MetaProgrammingData)this.hashTable[keys[i]];
                        var aux = metaProgrammingData.ToString();
                        if (aux != string.Empty)
                        {
                            a += metaProgrammingData.ToString();
                            if (i < keys.Count - 1) 
                                a += ",";
                        }
                    }
                }
                
                if(a[a.Length-1] == ',')
                    a = a.Substring(0, a.Length-1);

                a += "]";

                // Parameter #3: The control is at the root?
                if (this.inRoot)
                    a += ",true";
                else
                    a += ",false";

                // Parameter #4: The control is the root control?
                if (this.isRoot)
                    a += ",true";
                else
                    a += ",false";

                // Parameter #5: The prototype of control is declared?
                if (this.control is JavaScriptCodeBehind.UserControl || this.control is JavaScriptCodeBehind.Page)
                    a += ", typeof(" + this.control.GetType().BaseType.Name + ")";
                else
                    a += ", undefined";

                a += "]";

                return a;
            }
            else
            {
                return string.Empty;
            }
        }

    }    
}
